const vTrack = {
  mounted(el, binding) {
    const throttle = (callback, delay = 500) => {
      let timer = null
      let lastExecTime = 0
      return function (...args) {
        const now = Date.now()
        if (now - lastExecTime >= delay) {
          callback.apply(this, args)
          lastExecTime = now
        } else {
          clearTimeout(timer)
          timer = setTimeout(() => {
            callback.apply(this, args)
            lastExecTime = Date.now()
          }, delay)
        }
      }
    }
    el.addEventListener(
      'click',
      throttle(() => {
        const trackData = { ...binding.value }
        window.$apis.track(trackData)
      })
    )
  }
}

export default vTrack
